package com.gdit.zax.servletjsp;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.gdit.zax.dao.VideoDao;
import com.gdit.zax.entitys.VideoInfo;
import com.gdit.zax.factory.DAOFactory;
import com.gdit.zax.utils.DBUtil;
import com.gdit.zax.utils.Repost;
import com.gdit.zax.utils.TextUtil;

public class VPlayer extends HttpServlet {
	private String outstring1 ="<!DOCTYPE html><html><head><title>Video Player CNZ.CO</title><link href=\"playersrc/video-js.css\" rel=\"stylesheet\" type=\"text/css\"><script src=\"playersrc/video.js\"></script><style type=\"text/css\">*{margin: 0;padding: 0;border: 0;background-color: transparent;}</style><script>_V_.options.flash.swf = \"playersrc/video-js.swf\";</script></head><body><video id=\"example_video_1\" class=\"video-js vjs-default-skin\" controls preload=\"none\" width=\"100%\" height=\"200\"poster=\"";
	private String outstring2 ="\" data-setup=\"{}\"><source src=\"";
	private String outstring3 ="\" type='video/mp4' /><track kind=\"captions\" src=\"playersrc/captions.vtt\" srclang=\"en\" label=\"English\" /></video></body></html>";
	
	//vplayer?id=123&accesstoken=dfasdf
	//accesstoken可选
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int vid = TextUtil.String2int(request.getParameter("id"),0);
		String accesstoken = request.getParameter("accesstoken");//可选
		
		
		Connection conn = DBUtil.getConnection();
		if (conn==null) {
			outhtml(response, "videopics/default.jpg", "videos/default.mp4");
			return;
		}
		
		if (vid==0) {
			outhtml(response, "videopics/default.jpg", "videos/default.mp4");
			return;
		}
		
		DAOFactory dao = DAOFactory.init(conn, request, response);
		VideoDao vdao = dao.getVideoDao();
		VideoInfo vinfo = vdao.getvideoinfo(vid);
		if (vinfo==null) {
			outhtml(response, "videopics/default.jpg", "videos/default.mp4");
			return;			
		}
		
		
		accesstoken = accesstoken==null?"":accesstoken;
		String picpath = TextUtil.isEmpty(vinfo.getPic())?"videopics/default.jpg": vinfo.getPic();
		String videopath = TextUtil.isEmpty(vinfo.getUrl())?"videopics/default.mp4": vinfo.getUrl() + "?type=0&accesstoken=" + accesstoken;
		
		outhtml(response, picpath, videopath);
	}
	
	private void outhtml (HttpServletResponse response,String picpath,String videopath) {
		String mmout = outstring1 + picpath  + outstring2 + videopath + outstring3;
		PrintWriter out;
		try {
			out = response.getWriter();
			out.print(mmout);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
